package agent;

import agent.container.ProxyClient;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.ftp.SimpleFtpServer;
import cn.hutool.log.StaticLog;
import common.container.SwitchContainer;
import common.pojo.Constant;
import common.pojo.TunnelInfoVO;
import common.utils.NetUtil;
import common.utils.PropertiesUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author gitama
 * @version 1.0
 * @date 2022/12/05
 */
@Slf4j
public class AgentStartApp {

    private static String VERSION = PropertiesUtil.getProperty("app.version");
    private static String HOST = PropertiesUtil.getProperty("server.host");
    private final static String PROXY_IP = PropertiesUtil.getProperty("proxy.ip");
    private final static Integer PROXY_PORT = Integer.parseInt(PropertiesUtil.getProperty("proxy.port"));
    public static String DOMAIN = null;
    public static String TOKEN = PropertiesUtil.getProperty("proxy.token");
    ;
    public static String PROTOCOL = null;


    public static void main(String[] args) {
        createFtpServer();
        String url = String.format(HOST + Constant.API_GET_CHANNEL_INFO + "?token=%s", TOKEN);
        String data = NetUtil.doGet(url);
        JSONObject jsonObject = JSON.parseObject(data);
        if (jsonObject.getBoolean("success")) {
            //连接代理服务端
            SwitchContainer container = new ProxyClient(PROXY_IP, PROXY_PORT,TOKEN);
            container.start();
            StaticLog.info("连接成功:" + PROXY_IP + ":" + PROXY_PORT);
        } else {
            StaticLog.info("无用秘钥");
            System.exit(0);
        }

    }

    public static class ArgsParser {

        @Parameter(names = {"-h", "--help"}, description = "Show this help")
        private boolean help;

        @Parameter(names = {"-v", "--version"}, description = "Program version")
        private boolean version;

        @Parameter(names = {"-k", "--key"}, description = "auth token key")
        private String key;

        public ArgsParser parse(String[] args) {

            ArgsParser options = new ArgsParser();

            JCommander jc = JCommander.newBuilder().addObject(options).build();

            try {
                jc.parse(args);
                if (options.help) {
                    jc.usage();
                    System.exit(0);
                } else if (options.version) {
                    System.out.println(VERSION);
                    System.exit(0);
                }
            } catch (ParameterException parameterException) {
                jc.usage();
                System.exit(0);
            }

            if (options.key == null) {
                System.out.println("没有秘钥");
                System.exit(0);
            }

            return options;
        }

        public boolean isHelp() {
            return help;
        }

        public boolean isVersion() {
            return version;
        }

        public String getKey() {
            return key;
        }
    }

    public static void createFtpServer() {
        String dirPath = "/";
        List<Authority> authorities = new ArrayList<Authority>();
        //增加写权限
        authorities.add(new WritePermission());
        BaseUser user = new BaseUser();
        user.setName("root");
        user.setPassword("root");
        user.setHomeDirectory(dirPath);
        user.setAuthorities(authorities);
        Boolean isExist = FileUtil.exist(dirPath);
        if (!isExist) {
            FileUtil.mkdir(dirPath);
        }
        SimpleFtpServer
                .create()
                .setPort(6666)
                .addUser(user)
                .start();
    }

}
